﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace solitaire
{
    public partial class StartPile : UserControl, ICardContainer
    {
        private List<Card> _cards = new List<Card>();
        private Card _downCard = new Card(CardValue.Ace, CardSuit.Spades); //dummy card to represent the face down card of the start pile
        public StartPile()
        {
            InitializeComponent();
            _downCard.FaceUp = false;
        }

        public event EventHandler<CardReturnEventArgs> StartCardClicked;

        #region ICardContainer Members

        public Card RetrieveTopCard()
        {
            if (TotalCards == 0) return null;
            Card c = _cards[_cards.Count - 1].Clone();
            _cards.RemoveAt(_cards.Count - 1);
            return c;
        }

        public void AddCard(Card c)
        {
            if (_cards.Count > 0)
            {
                //change the click event to the most recent top card
                _cards[_cards.Count - 1].MouseLeftButtonDown -= new MouseButtonEventHandler(c_MouseLeftButtonDown);
            }
            c.FaceUp = false;
            c.MouseLeftButtonDown += new MouseButtonEventHandler(c_MouseLeftButtonDown);
            c.FromContainer = this;
            _cards.Add(c);
            UpdateCardView();
        }

        #endregion

        private void UpdateCardView()
        {
            ClearAllCardsFromUI();
            double p = .2;
            int z = 1;
            foreach (Card c in _cards)
            {
                Canvas.SetZIndex(c, z++);
                Canvas.SetTop(c, p -= .2);
                Canvas.SetLeft(c, p);
                this.LayoutRoot.Children.Add(c);
            }

        }

        void c_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (StartCardClicked != null)
            {
                Card c = RetrieveTopCard().Clone();
                StartCardClicked(this, new CardReturnEventArgs(c));
                if (_cards.Count > 0)
                {
                    //Add the click event back to the most recent top card
                    _cards[_cards.Count - 1].MouseLeftButtonDown += new MouseButtonEventHandler(c_MouseLeftButtonDown);
                }
                UpdateCardView();
            }
            e.Handled = true;
        }

        private void ClearAllCardsFromUI()
        {
            List<Card> killList = new List<Card>();

            foreach (UIElement eu in LayoutRoot.Children)
            {
                if (eu is Card) killList.Add((Card)eu);
            }
            while (killList.Count > 0)
            {
                LayoutRoot.Children.Remove(killList[0]);
                killList.RemoveAt(0);
            }
        }

        #region ICardContainer Members

        public void Clear()
        {
            ClearAllCardsFromUI();
            _cards.Clear();
        }

        public Card RetrieveCard(Card c)
        {
            _cards.Remove(c);
            UpdateCardView();
            return c;
        }

        #endregion

        #region ICardContainer Members


        public bool IsInside(Point p)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ICardContainer Members


        public Card[] RetrieveCardAndDescendants(Card c)
        {
            throw new NotImplementedException();
        }
        public int TotalCards
        {
            get { return _cards.Count; }
        }
        #endregion
    }
}
